﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using AForge.Imaging;
using System.Windows.Forms;
using System.Reflection;

namespace MotionDetect
{
    partial class Form1
    {
        int stride = 3 * 160;
        float Href, Sref;
        int X, Y;
        int talalatok = 0;

        public void ProcessImage(Bitmap bm)
        {
            unsafe
            {
                BitmapData bmd = bm.LockBits(new Rectangle(0, 0, bm.Width, bm.Height),
                    System.Drawing.Imaging.ImageLockMode.ReadOnly, bm.PixelFormat);
                int PixelSize = 3;
                int x_osszeg = 0;
                int x_db = 1;
                int y_osszeg = 0;
                int y_db = 1;
                for (int y = 0; y < bmd.Height; y++)
                {

                    byte* row = (byte*)bmd.Scan0 + (y * bmd.Stride);

                    for (int x = 0; x < bmd.Width; x++)
                    {
                        int blue = row[x * PixelSize];
                        int green = row[x * PixelSize + 1];
                        int red = row[x * PixelSize + 2];
                        Color c = Color.FromArgb(red, green, blue);
                        float H = c.GetHue();
                        float H2 = c.GetHue();
                        float H3 = c.GetHue();

                        float S = c.GetSaturation();
                        float S2 = c.GetSaturation();
                        float S3 = c.GetSaturation();
                        
                        
                        if (H > Href - 2 & H < Href + 2 & S > Sref - 0.1 &
                            H2 > Href - 2 & H2 < Href + 2 & S2 > Sref - 0.1 &
                            H3 > Href - 2 & H3 < Href + 2 & S3 > Sref - 0.1
                            )
                        {
                            row[x * PixelSize] = 0;
                            row[x * PixelSize + 1] = 0;
                            row[x * PixelSize + 2] = 255;
                            x_osszeg += x;
                            x_db++;
                            y_osszeg += y;
                            y_db++;
                        }

                    }
                }
                bm.UnlockBits(bmd);
                X = x_osszeg / x_db;
                Y = y_osszeg / y_db;
                talalatok = x_db + y_db;
                UpdateText(label1,"Text",X + "x" + Y);
            }


        }//end processimagemethod

        private delegate void UpdateTextDelegate(Control control, string propertyName, object propertyValue);

        public static void UpdateText(Control control, string propertyName, object propertyValue)
        {
            if (control.InvokeRequired)
            {
                control.Invoke(new UpdateTextDelegate(UpdateText), new object[] { control, propertyName, propertyValue });
            }
            else
            {
                control.GetType().InvokeMember(propertyName, BindingFlags.SetProperty, null, control, new object[] { propertyValue });
            }
        }

        

    }
}
